package dev.ece.suit.ui;

import java.io.FileNotFoundException;
import java.io.IOException;

import dev.ece.core.shader.DefaultTextureShader;
import dev.ece.suit.ui.shader.AnimateImageTextureShader;
import dev.ece.util.lib.IntOffset;
import dev.ece.util.lib.IntRectangle;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector2i;
import dev.ece.util.tools.Camera;

public class AnimateImage extends ContentItem {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8646845961146084651L;

	private int currentOffsetIndex;
	
	private Vector2f[] offsets;
	
	private int currentFrameIndex;
	
	private int frames = 5;
	
	public AnimateImage(String background, float width, float height, IntOffset centerClipping, Vector2i[] offsets) throws IOException {
		super(background, width, height, centerClipping);
		this.setOffsets(offsets);
	}

	public AnimateImage(String background, float width, float height, IntRectangle centerClipping, Vector2i[] offsets) throws IOException {
		super(background, width, height, centerClipping);
		this.setOffsets(offsets);
	}

	public AnimateImage(String background, float width, float height, Vector2i[] offsets) throws IOException {
		super(background, width, height);
		this.setOffsets(offsets);
	}

	public AnimateImage(String background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, Vector2i[] offsets) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		this.setOffsets(offsets);
	}

	public AnimateImage(String background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, Vector2i[] offsets) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		this.setOffsets(offsets);
	}

	public AnimateImage(String background, IntRectangle textureClipping, float width, float height, Vector2i[] offsets) throws IOException {
		super(background, textureClipping, width, height);
		this.setOffsets(offsets);
	}

	@Override
	protected DefaultTextureShader createDefaultTextureShader() {
		try {
			return AnimateImageTextureShader.getDefaultAnimateImageTextureShader();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void onLogic(Camera camera) {
		setCurrentFrameIndex((getCurrentFrameIndex() + 1) % getFrames());
		if(getCurrentFrameIndex() == 0) {
			setCurrentOffsetIndex((getCurrentOffsetIndex() + 1) % getOffsets().length);
		}
	}

	@Override
	protected void glUseShader() {
		super.glUseShader();
		if(getCurrentOffsetIndex() > -1) {
			((AnimateImageTextureShader) getDefaultTextureShader()).glOffset(getOffsets()[getCurrentOffsetIndex()]);
		}
	}

	public Vector2f[] getOffsets() {
		return offsets;
	}

	public synchronized void setOffsets(Vector2i[] offsets) {
		if(offsets == null || offsets.length == 0) {
			throw new IllegalArgumentException("offsets不能是null，length不能为0");
		}
		this.offsets = new Vector2f[offsets.length];
		Vector2i offset = null;
		for(int i=0; i<offsets.length; i++) {
			offset = offsets[i];
			this.offsets[i] = new Vector2f(
					offset.getX() / (float) getBackground().getWidth(), 
					offset.getY() / (float) getBackground().getHeight());
		}
		this.setCurrentOffsetIndex(-1);
	}

	public int getFrames() {
		return frames;
	}

	public synchronized void setFrames(int frames) {
		this.frames = frames;
		this.setCurrentFrameIndex(-1);
		this.setCurrentOffsetIndex(-1);
	}

	public int getCurrentOffsetIndex() {
		return currentOffsetIndex;
	}

	public void setCurrentOffsetIndex(int currentOffsetIndex) {
		this.currentOffsetIndex = currentOffsetIndex;
	}

	public int getCurrentFrameIndex() {
		return currentFrameIndex;
	}

	protected void setCurrentFrameIndex(int currentFrameIndex) {
		this.currentFrameIndex = currentFrameIndex;
	}
	
}
